# -*- coding: utf-8 -*-

from qiniu import Auth, put_file, etag
import qiniu.config
import base64
import io
#需要填写你的 Access Key 和 Secret Key
access_key = 'cRn_IgG74ge-1BA2g05pno_WcaEUe3I8Fdn7TRK5'
secret_key = 'GBKRnHy4TECD86SIa0us4LBwEm-kPNWEXr74cmvv'

#构建鉴权对象
q = Auth(access_key, secret_key)

#要上传的空间
bucket_name = 'python7-dengdeyan'


#上传到七牛后保存的文件名
key = 'my-python-logo.png'

#生成上传 Token，可以指定过期时间等
token = q.upload_token(bucket_name, key, 3600)

#要上传文件的本地路径
basestr = 'data:image/jpeg;base64, /9j/4AAQSkZJRgABAgAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCABGAKADASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJma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c56Z46VAPINlC+2TLy4HzcBfp0zt4z60UUASulsJ70MspSNMkbzlj3yc89uue9IiQlrFT5m+T75DH9PT5sdMcUUUAMYQfZrp1WT5ZAsYLcZ7EjvznrmphHbfbkjAm2+TuY+YcnjjnOemeOlFFAEA8g2UL7ZMvLgfNwF+nTO3jPrUrpbCe9DLKUjTJG85Y98nPPbrnvRRQB//9k='
data = base64.b64decode(basestr)
imgFile = io.StringIO(data) #缓存图片
img = Image.open(imgFile)

# localfile = 'C:/Users/mayn/Pictures/Saved Pictures/20130801101917033.jpg'

ret, info = put_file(token, key, img)
print(info)
assert ret['key'] == key
assert ret['hash'] == etag(localfile)

